package com.dcits.mvc.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.dcits.business.userconfig.UserCommonConfig;
import com.dcits.business.userconfig.UserSpace;
import com.dcits.dto.DTOParseUtil;
import com.dcits.mvc.base.BaseController;
import com.dcits.mvc.common.model.UserConfig;
import com.dcits.mvc.common.service.UserConfigService;
import com.dcits.mvc.common.validator.UserKeyValidator;
import com.jfinal.aop.Before;

@Before(UserKeyValidator.class)
public class UserConfigController extends BaseController {
	
	private static UserConfigService service = new UserConfigService();
	
	public void login() {
		UserConfig config = service.findByKey(getPara("userKey"));
		UserSpace space = UserSpace.getUserSpace(config.getUserKey());
		if (space == null) {
			//初始化用户空间
			UserSpace.addUserSpace(config);
		}
		renderSuccess(config, "success");
	}
	
	public void add () {
		String userKey = getPara("userKey");		
		service.save(userKey);
		renderSuccess(null, "添加成功!");
	}
	
	public void del() {
		service.delbyKey(getPara("userKey"));
		renderSuccess(null, "删除成功!");
	}
	
	public void getUserSetting() {
		UserConfig config = service.findByKey(getPara("userKey"));
		renderSuccess(JSONObject.parseObject(config.getConfigInfoJson(), new TypeReference<UserCommonConfig>(){}), "success");		
	}
	
	public void listSpace() {
		renderSuccess(DTOParseUtil.parseUserSpace(), "");
	}
}
